//
//  MCBonusRecordViewController.m
//  TLYL
//
//  Created by MC on 2017/11/1.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCBonusRecordViewController.h"
#import "MCBonusRecordSlideView.h"
#import "MCGetDividentsListModel.h"
#import "MCRecordTool.h"
#import "MCBonusGetSystemTypeModel.h"
#import "MCMMIputView.h"
#define MORENCOUNT 15

@interface MCBonusRecordViewController ()

@property (nonatomic,strong)MCBonusRecordSlideView *segmentedView;
@property(nonatomic, strong)MCNaviSelectedPopView *popView1;
@property(nonatomic, weak)  MCMMIputView * viewPop1;
@property(nonatomic, weak)  DatePickerView *datePicker1;
@property(nonatomic, assign)BOOL  isShowPopView1;

@property (nonatomic,strong) NSDate *start_minDate1;
@property (nonatomic,strong) NSDate *start_maxDate1;
@property (nonatomic,strong) NSDate *end_minDate1;
@property (nonatomic,strong) NSDate *end_maxDate1;

@property(nonatomic, strong) NSString * statTime1;
@property(nonatomic, strong) NSString * endTime1;

@property(nonatomic, assign) BOOL isHadRefreashUI;

@property(nonatomic, assign) BOOL IsHistory;
@property(nonatomic, strong) MCBaseGetSystemTypeModel * SystemType;
@property(nonatomic, strong) MCBaseGetSystemTypeModel * selected_SystemType;
@property(nonatomic, strong) MCBonusGetSystemTypeModel * bonusGetSystemTypeModel;
@property(nonatomic, strong) ExceptionView * exceptionView;
@end

@implementation MCBonusRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    
    [self setNav];
    
    [self createUI];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.translucent = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self dismissAllPopView1];
    
}




#pragma mark==================setProperty======================
-(void)setProperty{
    self.view.backgroundColor=RGB(231, 231, 231);
    self.navigationItem.title = @"分红记录";
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    
    _isShowPopView1=NO;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    _IsHistory=NO;
    _statTime1=[NSString stringWithFormat:@"%@ 00:00:00",currentDateStr];
    _endTime1=[NSString stringWithFormat:@"%@ 23:59:59",currentDateStr];
//    _CurrentPageIndex=@"1";
//    _CurrentPageSize=[NSString stringWithFormat:@"%d",MORENCOUNT];
    
    self.start_minDate1 =[MCRecordTool getLaterDateFromDate:[NSDate date] withYear:0 month:0 day:-3];
    self.start_maxDate1 = [NSDate date];
    self.end_minDate1 = [NSDate date];
    self.end_maxDate1 = [NSDate date];

    self.popView1.label3.text = [NSString stringWithFormat:@"%@",currentDateStr];
    self.popView1.label4.text = [NSString stringWithFormat:@"%@",currentDateStr];
    _IsHistory=NO;
    
    
    /*
     * 先确保SystemType有值
     */
    MCBonusGetSystemTypeModel * bonusGetSystemTypeModel = [MCBonusGetSystemTypeModel sharedMCBonusGetSystemTypeModel];
    _SystemType = bonusGetSystemTypeModel.R_bonusDataSource[0];
    
}


#pragma mark ====================设置导航栏========================
-(void)setNav{
    
    UIButton *rightBtn = [[UIButton alloc] init];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"debss"] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn sizeToFit];
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}


#pragma mark==================createUI======================
-(void)createUI{
    
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    
    self.view.backgroundColor=RGB(231,231,231);
    [self checkSystemType];
    
}

-(void)checkSystemType{

    __weak __typeof__ (self) wself = self;
    MCBonusGetSystemTypeModel * bonusGetSystemTypeModel = [MCBonusGetSystemTypeModel sharedMCBonusGetSystemTypeModel];
    if (bonusGetSystemTypeModel.bonusDataSource.count<1) {
        
        [BKIndicationView showInView:self.view];
        MCBonusGetSystemTypeModel * bonusGetSystemTypeModel = [MCBonusGetSystemTypeModel sharedMCBonusGetSystemTypeModel];
        [bonusGetSystemTypeModel refreashDataAndShow];
        self.bonusGetSystemTypeModel=bonusGetSystemTypeModel;
        bonusGetSystemTypeModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
            [BKIndicationView dismiss];
            wself.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeRequestFailed];
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeRequestFailed handler:^(ExceptionViewAction *action) {
                [wself.exceptionView dismiss];
                wself.exceptionView = nil;
                [wself checkSystemType];
            }];
            [wself.exceptionView addAction:action];
            [wself.exceptionView showInView:wself.view];
        };
        bonusGetSystemTypeModel.callBackSuccessBlock = ^(id manager) {
            [BKIndicationView dismiss];
            [wself.bonusGetSystemTypeModel setDataSource:manager];
            NSArray * arr = manager;
            if (arr.count<1) {
                //无数据
                self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
                [self.exceptionView showInView:self.view];
            }else{
                [wself checkSystemType];
            }
        };
    }else{
        MCBaseGetSystemTypeModel * Bmodel = bonusGetSystemTypeModel.R_bonusDataSource[0];
        _SystemType=Bmodel;
        [self createSlide];
    }
    
    
}
#pragma mark ================================创建Slide
-(void)createSlide{
    
//    NSArray *array = [NSArray arrayWithObjects:@"直属下级分红",@"自身分红",@"日分红合计",nil];
    NSArray *array = [NSArray arrayWithObjects:@"直属下级分红",@"自身分红",nil];

    _segmentedView = [MCBonusRecordSlideView segmentControlViewWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight)];
    
    _segmentedView.normalColor = RGB(102,102,102);
    _segmentedView.selectedColor = MC_THEME_COLOR;
    _segmentedView.SlideSelectedColor=MC_THEME_COLOR;
    
    _segmentedView.titleArray = array;
    _segmentedView.selectedTitleArray = array;
    [self.view addSubview:_segmentedView];
    
    
}
-(void)rightBtnAction{
    

    self.selected_SystemType=self.SystemType;
    
    self.popView1.label2.text=self.SystemType.Name;

    if (_isShowPopView1) {
        _isShowPopView1=NO;
        [self dismissAllPopView1];
        
    }else{
        _isShowPopView1=YES;
        
        typeof(self) weakself = self;
        
        self.popView1.block = ^(NSInteger type) {

#pragma mark-记录选择
            if (type==1) {
                
                [weakself.datePicker1 removeFromSuperview];
                
                weakself.viewPop1.dataArray = @[@"当前记录",@"历史记录"];
                [weakself.viewPop1 show];
                weakself.viewPop1.cellDidSelected = ^(NSInteger i) {
                    
                    [weakself.viewPop1 hiden];
                    weakself.popView1.label1.text = weakself.viewPop1.dataArray[i];
                    if (i == 0) {
                        
                        weakself.IsHistory = NO;
                        weakself.start_minDate1 =[MCRecordTool getLaterDateFromDate:[NSDate date] withYear:0 month:0 day:-3];
                        weakself.start_maxDate1 = [NSDate date];
                        weakself.end_minDate1 = [NSDate date];
                        weakself.end_maxDate1 = [NSDate date];
                        
                    } else {
                        
                        weakself.IsHistory = YES;
                        
                        weakself.start_minDate1 = [MCRecordTool getLaterDateFromDate:[NSDate date] withYear:0 month:-3 day:-3];
                        weakself.start_maxDate1 = [MCRecordTool getLaterDateFromDate:[NSDate date] withYear:0 month:0 day:-4];
                        
                        weakself.end_minDate1 = [MCRecordTool getLaterDateFromDate:[NSDate date] withYear:0 month:0 day:-4];
                        weakself.end_maxDate1 = [MCRecordTool getLaterDateFromDate:[NSDate date] withYear:0 month:0 day:-4];
                        
                    }
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
                    NSString *endDateStr = [dateFormatter stringFromDate:weakself.end_maxDate1];
                    
                    weakself.statTime1 =[NSString stringWithFormat:@"%@ 00:00:00",endDateStr];
                    weakself.endTime1 = [NSString stringWithFormat:@"%@ 23:59:59",endDateStr];
                    weakself.popView1.label3.text = [NSString stringWithFormat:@"%@",endDateStr];
                    weakself.popView1.label4.text = [NSString stringWithFormat:@"%@",endDateStr];
                    //                    weakself.popView.label3.text = [NSString stringWithFormat:@"%@ 00:00:00",endDateStr];
                    //                    weakself.popView.label4.text = [NSString stringWithFormat:@"%@ 23:59:59",endDateStr];
                };
                
                
               
                
              
                
                
#pragma mark-类型选择
            }else if (type==2){
                
                [weakself.datePicker1 removeFromSuperview];
                
                MCBonusGetSystemTypeModel * bonusGetSystemTypeModel = [MCBonusGetSystemTypeModel sharedMCBonusGetSystemTypeModel];
                
                NSArray * arr = bonusGetSystemTypeModel.R_nameDataSource;
                NSArray * Marr =bonusGetSystemTypeModel.R_bonusDataSource;
                if (arr.count<1) {
                    return ;
                }
                weakself.viewPop1.dataArray =  arr;
                [weakself.viewPop1 show];
                weakself.viewPop1.cellDidSelectedTop = ^(NSInteger i) {
                    
                    MCBaseGetSystemTypeModel * model =  Marr[i];
                    
                    weakself.selected_SystemType=model;
                    weakself.popView1.label2.text=arr[i];
                    [weakself.viewPop1 hiden];
                    
                };
#pragma mark-开始时间
            }else if (type==3){
                
                [weakself.viewPop1 hiden];
                
                weakself.datePicker1.minDate=weakself.start_minDate1;
                weakself.datePicker1.maxDate=weakself.start_maxDate1;
                weakself.datePicker1.showDate = [MCRecordTool getDateWithStr:weakself.statTime1];
                
                weakself.datePicker1.cancelBlock = ^{
                    
                    [UIView animateWithDuration:0.25 animations:^{
                        [weakself.datePicker1 removeFromSuperview];
                    }];
                };
                
                weakself.datePicker1.sureBlock = ^(NSString *selectDateStr) {
                    weakself.popView1.label3.text = [NSString stringWithFormat:@"%@",selectDateStr];
                    //                    weakself.popView.label3.text = [NSString stringWithFormat:@"%@ 00:00:00",selectDateStr];
                    weakself.statTime1 = [NSString stringWithFormat:@"%@ 00:00:00",selectDateStr];
                    weakself.end_minDate1=[MCRecordTool getDateWithStr:[NSString stringWithFormat:@"%@ 00:00:00",selectDateStr]];
                    
                    [UIView animateWithDuration:0.25 animations:^{
                        [weakself.datePicker1 removeFromSuperview];
                        
                    }];
                };
                
#pragma mark-结束时间
            }else if (type==4){
                
                
                weakself.datePicker1.minDate=weakself.end_minDate1;
                weakself.datePicker1.maxDate=weakself.end_maxDate1;
                NSString * endTime = [weakself.endTime1 stringByReplacingOccurrencesOfString:@"23:59:59" withString:@"00:00:00"];
                weakself.datePicker1.showDate = [MCRecordTool getDateWithStr:endTime];
                
                [weakself.viewPop1 hiden];
                weakself.datePicker1.cancelBlock = ^{
                    
                    [UIView animateWithDuration:0.25 animations:^{
                        [weakself.datePicker1 removeFromSuperview];
                    }];
                };
                
                weakself.datePicker1.sureBlock = ^(NSString *selectDateStr) {
                    weakself.start_maxDate1=[MCRecordTool getDateWithStr:[NSString stringWithFormat:@"%@ 00:00:00",selectDateStr]];
                    weakself.popView1.label4.text = [NSString stringWithFormat:@"%@",selectDateStr];
                    //                    weakself.popView.label4.text = [NSString stringWithFormat:@"%@ 23:59:59",selectDateStr];
                    weakself.endTime1 = [NSString stringWithFormat:@"%@ 23:59:59",selectDateStr];
                    [UIView animateWithDuration:0.25 animations:^{
                        [weakself.datePicker1 removeFromSuperview];
                        
                    }];
                };
                
#pragma mark-搜索
            }else if (type==8){
                
                weakself.isShowPopView1=NO;
                [weakself dismissAllPopView1];
                if (weakself.selected_SystemType) {
                    weakself.SystemType=weakself.selected_SystemType;
                }
                [weakself setBonusRecordProperty];
                
                [weakself.segmentedView.xiaJiCell refreashDataFromSearchBar];
                
                [weakself.segmentedView.mySelfCell refreashDataFromSearchBar];
                
                [weakself.segmentedView.sumCell refreashDataFromSearchBar];
            }
        };
        
        self.popView1.frame= CGRectMake(0, kStatusBarAndNavigationBarHeight, G_SCREENWIDTH, G_SCREENHEIGHT);
        [self.popView1 showPopView];
    }
    
}


- (MCNaviSelectedPopView *)popView1{
    
    if (_popView1 == nil) {
        MCNaviSelectedPopView * popView1 = [[MCNaviSelectedPopView alloc]InitWithType:MCNaviSelectedPopType_BonusRecord];
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1)];
        [popView1 addGestureRecognizer:tap1];
        [self.navigationController.view addSubview:popView1];
        _popView1 = popView1;
    }
    return _popView1;
}

- (MCMMIputView *)viewPop1{
    if (_viewPop1 == nil) {
        
        MCMMIputView *viewStatus = [[MCMMIputView alloc] init];
        [self.navigationController.view addSubview:viewStatus];
        _viewPop1 = viewStatus;
    }
    return _viewPop1;
}
- (DatePickerView *)datePicker1{
    
    if (_datePicker1 == nil) {
        DatePickerView *datePicker1 =[[[NSBundle mainBundle] loadNibNamed:@"DatePickerView" owner:self options:0] lastObject];
        datePicker1.frame =CGRectMake(0,self.view.frame.size.height  - 200 + kStatusBarAndNavigationBarHeight, self.view.frame.size.width, 200);
        datePicker1.Datetitle =@"日期选择";
        [self.navigationController.view addSubview:datePicker1];
        _datePicker1 = datePicker1;
    }
    return _datePicker1;
}
- (void)tap1{
    _isShowPopView1=NO;
    [self dismissAllPopView1];
}
-(void)dismissAllPopView1{
    [self.popView1 dismiss];
    [self.viewPop1 hiden];
    [self.datePicker1 removeFromSuperview];
}

-(void)setBonusRecordProperty{

    MCBonusRecordProperty * PersonProperty = [MCBonusRecordProperty sharedMCBonusRecordProperty];
    PersonProperty.BeginTime=self.statTime1;
    PersonProperty.EndTime=self.endTime1;
    PersonProperty.CurrentPageIndex=@"1";
    PersonProperty.CurrentPageSize=[NSString stringWithFormat:@"%d",MORENCOUNT];
    PersonProperty.IsHistory=_IsHistory;
    PersonProperty.SystemType=_SystemType;
    if (_popView1.tf1.text.length>0) {
        PersonProperty.UserName=_popView1.tf1.text;
    }else{
        PersonProperty.UserName=@"";
    }
}
@end



























